/**
 * JAVACC DEMO 1.0
 */
package com.apache.passport.service.plugins;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.service.impl.redis.JedisSsoUtil;
import com.apache.cache.util.Validator;
import com.apache.database.constant.SystemTools;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.passport.common.DesUtils;
import com.apache.passport.common.PassportHelper;
import com.apache.passport.entity.LoginInfo;
import com.apache.passport.entity.Token;
import com.apache.passport.entity.UctUser;
import com.apache.passport.manager.UctUserManager;
import com.apache.passport.service.CacheHelper;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * description:  外部系统登录(socket形式,webservice)
 * 此种方式只返回token,下次手机端放的时候,都需要写到token进行传输
 * 例如,手机客户端,c#界面登录,以及其他语言的登录,全部使用这种形式或webservice
 *
 * @author Hou Dayu 创建时间：2016-12-25
 */
public class UserOwSocketLoginPlugin extends SuperPluginConnector {

    private UctUserManager uctUserManager;

    private DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    public void setUctUserManager(UctUserManager uctUserManager) {
        this.uctUserManager = uctUserManager;
    }

    /**
     * TODO 例如手机客户端,C# 等语言的登录
     * 下次手机端放的时候,都需要写到token进行传输
     *
     * @see com.apache.api.manager.PluginConnector#execute(com.apache.api.vo.ParamsVo)
     */
    @Override
    public Object execute(ParamsVo vo) throws Exception {
        String addressIP = String.valueOf(vo.getParams("addressIP"));
        ResultEntity result = new ResultEntity();
        result.setResult("true");

        String userEname = (String) vo.getParams("userEname");
        if (Validator.isEmpty(userEname)) {
            result.setEntity("F");
            result.setMessage("用户名不能为空");
            return result;
        }
        String upass = (String) vo.getParams("userPass");
        if (Validator.isEmpty(upass)) {
            result.setEntity("F");
            result.setMessage("密码不能为空");
            return result;
        }
        UctUser obj = (UctUser) getInfoByEname(userEname);
        if (!Validator.isEmpty(obj)) {
            //判断密码是否正确
            if ((obj.getUserPass()).equalsIgnoreCase(upass)) {
                String randNum = PassportHelper.getInstance().createRandomString(12);
                Token token = new Token();
                String tokenId = Validator.generate() + randNum;
                token.setTokenId(tokenId);
                token.setUserEname(DesUtils.getInstance().encrypt(userEname));
                token.setUserCname(obj.getUserCname());
                token.setLoginTime(df.format(new Date()));
                token.setEmail(DesUtils.getInstance().encrypt(obj.getEmail()));
                token.setMobile(DesUtils.getInstance().encrypt(obj.getMobile()));
                token.setUserId(obj.getUserId());
                result.setEntity(token);
                result.setMessage("登录成功");
                LoginInfo loginInfo = new LoginInfo();
                loginInfo.setLgnLastipaddress(addressIP);
                loginInfo.setUserEname(userEname);
                token.setLoginInfo(loginInfo);
                //存储缓存,且每次传输必须携带tokenId,作为访问系统的唯一标识 
                if (JedisSsoUtil.getInstance().isRedisCache()) {
                    JedisSsoUtil.getInstance().setLoginToken(tokenId, 0, token);
                } else {
                    CacheHelper.getInstance().getCache("loginToken")
                            .createCacheObject(tokenId, token);
                }
                return result;
            } else {
                result.setEntity("F");
                result.setMessage("密码错误");
                return result;
            }
        } else {
            result.setEntity("F");
            result.setMessage("用户不存在");
            return result;
        }
    }

    /**
     * description:  根据ename查询用户信息
     *
     * @return
     */
    public Object getInfoByEname(String userEname) {
        return super.getUserByEname(userEname);
//        if (Validator.isNull(userEname)) {
//            return null;
//        }
//        String sql = uctUserDao.getSql(3);// from UctUser where userEname :=userEname
//
//        String mobileLogin = SystemTools.getInstance().getValue("mobileLogin");
//        String emailLogin = SystemTools.getInstance().getValue("emailLogin");
//
//        if ("1".equals(mobileLogin)) {
//            sql += "or mobile :=userEname ";
//        }
//        if ("1".equals(emailLogin)) {
//            sql += "or email :=userEname ";
//        }
//
//        MethodParam param = new MethodParam("ByUserEname", "", sql,
//                "com.apache.passport.entity.UctUser");
//        param.setParams("userEname", userEname);
//        if ("1".equals(mobileLogin)) {
//            param.setParams("mobile", userEname);
//        }
//        if ("1".equals(emailLogin)) {
//            param.setParams("email", userEname);
//        }
//        return uctUserDao.selectSingle(param);
    }

}
